package com.chuangjiangx.member.stored.ddd.domain;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author cj-ch
 * @date 2018/4/8 下午6:21
 */
public class MbrRandomUtils {
    static final int MIN_LIMIT = 1;
    static final int MAX_LIMIT = 100;
    public static final String MBR_ORDER_PREFIX = "88";
    public static final String MBR_REFUND_ORDER_PREFIX = "87";
    /**
     * 卡券编号前缀
     */
    public static final String MBR_COUPON_NUMBER_PREFIX = "80";
    /**
     * 卡券核销码code前缀
     */
    public static final String MBR_COUPON_VERIFY_CODE_PREFIX = "81";
    /**
     * 商品取货码前缀
     */
    public static final String MBR_CLAIM_CODE_PREFIX = "82";

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyMMddHHmmss");

    private static final char[] NUMBER_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    public static final String generateNumbers(int length) {
        if (length < MIN_LIMIT || length > MAX_LIMIT) {
            throw new IllegalArgumentException("长度非法");
        }
        /**
         * 任何情况下都不要在多个线程间共享一个java.util.Random实例，而该把它放入ThreadLocal之中。
         * Java7在所有情形下都更推荐使用java.util.concurrent.ThreadLocalRandom——它向下兼容已有的代码且运营成本更低。
         */
        ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
        int range = NUMBER_CHARS.length;
        StringBuilder str = new StringBuilder();
        while (length-- > 0) {
            str.append(NUMBER_CHARS[threadLocalRandom.nextInt(range)]);
        }
        return str.toString();
    }

    /**
     * 生成会员订单号,长度24位
     *
     * @return
     */
    public static final String generateMbrOrderNumber() {
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = DATE_TIME_FORMATTER.format(localDateTime);
        return MBR_ORDER_PREFIX + format + generateNumbers(10);
    }

    /**
     * 生成会员退款订单号,长度24位
     *
     * @return
     */
    public static final String generateMbrRefundOrderNumber() {
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = DATE_TIME_FORMATTER.format(localDateTime);
        return MBR_REFUND_ORDER_PREFIX + format + generateNumbers(10);
    }

    /**
     * 生成卡券编号
     * @return 长度为18的字符串
     */
    public static final String generateMbrCouponNumber(){
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = DATE_TIME_FORMATTER.format(localDateTime);
        return MBR_COUPON_NUMBER_PREFIX + format+ generateNumbers(4);
    }
    /**
     * 生成卡券核销码
     * @return 长度为13的字符串
     */
    public static final String generateMbrCouponVerifyCode(){
        return MBR_COUPON_VERIFY_CODE_PREFIX + generateNumbers(11);
    }

    /**
     * 生成商品取货码
     * @return 长度为13的字符串
     */
    public static final String generateMbrClaimCode(){
        return MBR_CLAIM_CODE_PREFIX + generateNumbers(11);
    }

    public static void main(String[] args) {
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));
        System.out.println(generateNumbers(13));

    }
}
